package com.cy.核心编程.thead.basic.sync;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 线程同步：
 * 1.synchronized 可以用在方法上 例：public synchronized void
 * 2.Lock lock = new ReentrantLock();创建锁
 *   lock.lock();上锁
 *   lock.unlock();关锁
 */
public class Account {
    //账户余额
    private double balance;
    Lock lock = new ReentrantLock();

    public Account(double balance) {
        this.balance = balance;
    }

    public void getMoney() {
        //synchronized (this) {
        lock.lock();
            if (this.balance <= 0) {
                System.out.println("你没有足够的钱");
                return;
            }
            System.out.println("我要取钱了，目前还剩下：" + this.balance);
            this.balance -= 1000;
        //}
        lock.unlock();
        System.out.println("取完了，还剩：" + this.balance);

    }
}
